package org.mybatis.generator.plugins.lay;

import org.apache.log4j.Logger;
import org.mybatis.generator.api.GeneratedJavaFile;
import org.mybatis.generator.api.GeneratedJavaScriptFile;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.Interface;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.mybatis.generator.api.dom.xml.Document;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.plugins.lay.elements.LayuiMethodGenerator;
import org.mybatis.generator.plugins.vue.elements.VueMethodGenerator;

import java.util.ArrayList;
import java.util.List;

/**
 * cjianquan 2019/8/4
 */
public class MybatitLayuiPlugin extends PluginAdapter {

    private static final Logger logger = Logger.getLogger(MybatitLayuiPlugin.class);

    private static int SORT_ORDER = 10036;

    private String targetPackage;
    private String targetModel;
    private String targetProject;
    private String sortOrder ;

    private String platform;

    @Override
    public boolean validate(List<String> warnings) {
        targetPackage = properties.getProperty("targetPackage");
        targetModel = properties.getProperty("targetModel");
        targetProject = properties.getProperty("targetProject");
        sortOrder = properties.getProperty("sortOrder");

        platform = properties.getProperty("platform");
        return true;
    }

    @Override
    public List<GeneratedJavaFile> contextGenerateAdditionalJavaFiles(
            IntrospectedTable introspectedTable) {

        // 增加结束处
        return null;
    }

    @Override
    public boolean clientGenerated(Interface interfaze,
                                   TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        // 接口方法
        return true;
    }

    @Override
    public boolean sqlMapDocumentGenerated(Document document,
                                           IntrospectedTable introspectedTable) {
        return true;
    }

    @Override
    public boolean sqlMapSelectByExampleWithoutBLOBsElementGenerated(
            XmlElement element, IntrospectedTable introspectedTable) {
        return true;
    }

    @Override
    public boolean sqlMapSelectByExampleWithBLOBsElementGenerated(
            XmlElement element, IntrospectedTable introspectedTable) {
        return true;
    }

    @Override
    public List<GeneratedJavaScriptFile> contextGenerateAdditionalJavaScriptFiles() {
        // TODO Auto-generated method stub
        List<GeneratedJavaScriptFile> files = new ArrayList<GeneratedJavaScriptFile>(
                1);
        return files;
    }

    @Override
    public List<GeneratedJavaScriptFile> contextGenerateAdditionalJavaScriptFiles(
            IntrospectedTable introspectedTable) {
        String tableName = introspectedTable.getTableConfiguration()
                .getTableName();
        String tableLab = introspectedTable.getRemarks();
        while (tableName.lastIndexOf("_") != -1) {
            int index = tableName.lastIndexOf("_");
            tableName = tableName.replaceFirst(
                    "_" + tableName.substring(index + 1, index + 2), tableName
                            .substring(index + 1, index + 2).toUpperCase());
        }
        tableName=toLowerCase(tableName);
        List<GeneratedJavaScriptFile> files = new ArrayList<GeneratedJavaScriptFile>();


        LayuiMethodGenerator emg = new LayuiMethodGenerator(introspectedTable,
                targetModel,sortOrder);

        GeneratedJavaScriptFile showFile = new GeneratedJavaScriptFile(
                emg.getShowDialogMethod(), properties.getProperty("fileName",
                tableName + "Show.jsp"), "views/"+targetPackage, targetProject, true);
        GeneratedJavaScriptFile editFile = new GeneratedJavaScriptFile(
                emg.getEditDialogMethod(), properties.getProperty("fileName",
                tableName + "Form.jsp"), "views/"+targetPackage, targetProject, true);

        GeneratedJavaScriptFile listFile = new GeneratedJavaScriptFile(
                emg.getListPageMethod(), properties.getProperty("fileName",
                tableName + "List.jsp"), "views/"+targetPackage, targetProject, true);

        GeneratedJavaScriptFile searchFile = new GeneratedJavaScriptFile(
                emg.getSearchMethod(), properties.getProperty("fileName",
                tableName + "Search.html"), "tpl/"+targetPackage, targetProject, true);

        TextElement element = null;
        if("admin".equalsIgnoreCase(platform)){
            element = emg.getSqlMethodManage(targetPackage);
        }else if("agent".equalsIgnoreCase(platform)){
            element = emg.getSqlMethodAgent(targetPackage);
        }else if("business".equalsIgnoreCase(platform)){
            element = emg.getSqlMethodBusiness(targetPackage);
        }else{
            element = emg.getSqlMethodAdmin(targetPackage);
        }
        GeneratedJavaScriptFile sqlFile = new GeneratedJavaScriptFile(
                element, properties.getProperty("fileName",
                tableName + ".sql"), "views/"+targetPackage, targetProject, true);

        files.add(showFile);
        files.add(editFile);
        files.add(listFile);
        files.add(sqlFile);
        files.add(searchFile);

        SORT_ORDER++;
        return files;
    }


    /**
     * BaseUsers to baseUsers
     *
     * @param tableName
     * @return
     */
    protected String toLowerCase(String tableName) {
        StringBuilder sb = new StringBuilder(tableName);
        sb.setCharAt(0, Character.toLowerCase(sb.charAt(0)));
        return sb.toString();
    }

    /**
     * BaseUsers to baseUsers
     *
     * @param tableName
     * @return
     */
    protected String toUpperCase(String tableName) {
        StringBuilder sb = new StringBuilder(tableName);
        sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
        return sb.toString();
    }

}
